package org.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.domain.Customer;
import org.domain.Flight;
import org.domain.Report;
import org.domain.Reservation;

public class ReservationSystem {
	private List<Flight> flightsForTheEntireMonth;
	private List<Customer> customers;
	private List<Reservation> reservationsMadeToday;
	private List<Report> reports;
	private int revenueGeneratedToday;
	private int countOfReservationMade;
	
	public ReservationSystem() {
		flightsForTheEntireMonth = new ArrayList<Flight>();
		customers = new ArrayList<Customer>();
		reports = new ArrayList<Report>();
		reservationsMadeToday = new ArrayList<Reservation>();
		countOfReservationMade =0;
	}

	//getters
	public List<Customer> getCustomers() {
		return customers;
	}
	
	public List<Flight> getFlightsForTheEntireMonth() {
		return flightsForTheEntireMonth;
	}
	public List<Report> getReports() {
		return reports;
	}
	
	public List<Reservation> getReservationsMadeToday() {
		return reservationsMadeToday;
	}
	public int getCountOfReservationMade() {
		return countOfReservationMade;
	}
	
	public int getRevenueGeneratedToday() {
		return revenueGeneratedToday;
	}
	//Initialize creates flights for the next 30 days
	public void initialize() {		
		for(int i = 0; i<30; i++){
			String flightDate = getDate(i, Calendar.getInstance());
			
			flightsForTheEntireMonth.add(new Flight("SFO", "LAS", flightDate, 31, 180));
			flightsForTheEntireMonth.add(new Flight("LAS", "SFO", flightDate, 31, 180));
		}
		
		for(int i = 0; i<30; i++){
			Calendar date = Calendar.getInstance();
			date.setTime(new Date("04/06/2012"));
			String reportDate = getDate(i, date);
			reports.add(new Report(reportDate, 100, 18000));
		}
	}

	//parser to get date in mm/dd/yyyy format for the given date offset
	private String getDate(int dateOffset, Calendar date) {
		date.add(Calendar.DAY_OF_YEAR, dateOffset);
		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
		
		String flightDate = format.format(date.getTime());
		return flightDate;
	}

	//search flight given flight date, departure and destination
	public Flight searchFlight(String dateOfFlight, String flightDeparture,
			String flightDestination) {
		for (Flight flight : flightsForTheEntireMonth) {
			if(flight.getDateOfFlight().equals(dateOfFlight) &&
					flight.getFlightDeparture().equals(flightDeparture) && 
					flight.getFlightDestination().equals(flightDestination))
				return flight;
		}
		return null;
	}

	public void persistCustomer(Customer customer) {
		customers.add(customer);
	}

	public void persistReservation(Reservation reservation) {
		reservationsMadeToday.add(reservation);
		countOfReservationMade++;
		revenueGeneratedToday += reservation.getFlight().getPrice();
	}

	public void persistReport(Report report) {
		Report existingReport = searchReport(report.getDateOfReport());
		if(existingReport != null)
			reports.remove(existingReport);
		reports.add(report);
	}

	public Report searchReport(String dateOfReport) {
		for (Report report : getReports()) {
			if(report.getDateOfReport().equals(dateOfReport))
				return report;
		}
		return null;
	}

}
